import * as Cesium from "cesium";
import { Cesium3DTileFeature } from "cesium";
import { Model } from "cesium";
import { Label } from "cesium";
import { Cartographic } from "cesium";
import { Cesium3DTileset } from "cesium";
import { ScreenSpaceEventHandler } from "cesium";
import { Viewer,Cartesian3,Entity,HeightReference,CustomDataSource,LabelStyle } from "cesium";


interface optionss {
    imgSrc: string
}
export default class drawPoint {
    private viewer: Viewer;
    public options: optionss;
    private bill: any
    // private _handlers: null;
    private _handlers: Cesium.ScreenSpaceEventHandler;
    constructor(viewer: Viewer) {
        this.viewer = viewer
        if (this._handlers){
            //移除点击事件
            console.log('===',this._handlers)
            this._handlers.removeInputAction(Cesium.ScreenSpaceEventType.LEFT_CLICK)
        }
        this._handlers = new ScreenSpaceEventHandler(
            this.viewer.scene.canvas
          );
          
        // this.init()
    }
    draw(lon:number, lat: number){
        this.bill.position = Cartesian3.fromDegrees(lon, lat),
        this.bill.billboard = {
            image: this.options.imgSrc,
            heightReference: HeightReference.CLAMP_TO_GROUND,
            disableDepthTestDistance: Number.POSITIVE_INFINITY,
            scale: 0.4,
        }
        this.bill.label = {
            text: '点标记',
            font: '500 30px Helvetica',// 15pt monospace
            scale: 0.5,
            style: Cesium.LabelStyle.FILL,
            fillColor: Cesium.Color.WHITE,
            pixelOffset: new Cesium.Cartesian2(8, 25),   //偏移量
            showBackground: true,
            backgroundColor: new Cesium.Color(0.5, 0.6, 1, 1.0)
        }
        this.viewer.entities.add(this.bill)
    }
    drawSign(options:optionss) {
        if (this.viewer && options) {

            this.options = options
            var $this = this

            // left
            this._handlers.setInputAction(function (movement) {
                $this.bill = new Entity({})
                // var cartesian = $this.getCatesian3FromPX(movement.position);
                // if (cartesian && cartesian.x) {
                let cartesian = $this.viewer.scene.globe.pick($this.viewer.camera.getPickRay(movement.position), $this.viewer.scene);
                // 世界坐标转换为弧度
                let ellipsoid = $this.viewer.scene.globe.ellipsoid;
                let cartographic = ellipsoid.cartesianToCartographic(cartesian);

                // 弧度转换为经纬度
                let lon = Cesium.Math.toDegrees(cartographic.longitude);  // 经度
                let lat = Cesium.Math.toDegrees(cartographic.latitude);   // 纬度
                    $this.draw(lon,lat)
                // }
            }, Cesium.ScreenSpaceEventType.LEFT_CLICK);


        }
    }
    destroyed() {
        // this._handlers.destroy();
        console.log(this.options,'desot')
        if (this._handlers) {
            this._handlers.destroy();
            this._handlers.removeInputAction(Cesium.ScreenSpaceEventType.LEFT_CLICK)
            this._handlers = null
            console.log(this._handlers,'destory')
        }
    }
}